import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
  list = '/wmhb/wmContract/list',
  save='/wmhb/wmContract/add',
  edit='/wmhb/wmContract/edit',
  itemSave='/wmhb/wmContractProduct/add',
  itemEdit='/wmhb/wmContractProduct/edit',
  itemDelete = '/wmhb/wmContractProduct/delete',
  detail='/wmhb/wmContract/queryById',
  confirm = '/wmhb/wmContract/confirm',
  deleteOne = '/wmhb/wmContract/delete',
  deleteBatch = '/wmhb/wmContract/deleteBatch',
  importExcel = '/wmhb/wmContract/importExcel',
  exportXls = '/wmhb/wmContract/exportXls',
  delBomByPatch = '/wmhb/wmContractProduct/deleteByBatchNo',
  confirmBomByPatch = '/wmhb/wmContractProduct/confirm',
  loadTreeData = '/wmhb/wmContractProduct/loadTreeRoot',
  buBindList = '/wmhb/wmContractDep/queryBuContractItem',
  saveBuBind = '/wmhb/wmContractDep/saveBuContractItem',
  prodMapList = '/wmhb/wmContract/productTaskbookMap',
  prodBomList = '/wmhb/wmContract/contractBomList'
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({url: Api.list, params});

export const buBindList = (params)=>
  defHttp.get({url:Api.buBindList,params})

export const prodMapList = (params)=>
  defHttp.get({url:Api.prodMapList,params})

export const prodBomList = (params)=>
  defHttp.get({url:Api.prodBomList,params})

export const saveBuBind = (params)=>
  defHttp.post({url:Api.saveBuBind,params},{isTransformResponse:false})

export const detail =(params)=>
  defHttp.get({url:Api.detail,params})

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}


export const deleteByPatchNo = (params)=>{
  return defHttp.delete({url:Api.delBomByPatch,params},{joinParamsToUrl:true,isTransformResponse:false})
}

export const confirmContract = (params,handleSuccess)=>{
  return defHttp.put({url:Api.confirm,params},{joinParamsToUrl:true}).then(()=>{
    handleSuccess()
  })
}

export const confirmBatchBom = (params,handleSuccess)=>{
  createConfirm({
    iconType: 'warning',
    title: '确认导入',
    content: '是否确认导入销售清单',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.put({url:Api.confirmBomByPatch,params},{joinParamsToUrl:true}).then(()=>{
        handleSuccess()
      })
    }
  });
}

export const loadTreeData = (params) =>
  defHttp.get({url: Api.loadTreeData,params},{isTransformResponse:false});

export const loadContractDeptTree = (params)=>
  defHttp.get({url: Api.loadTreeData,params});


/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const itemDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      defHttp.delete({url: Api.itemDelete, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

export const saveOrUpdateItem = (params, isUpdate) => {
  let url = isUpdate ? Api.itemEdit : Api.itemSave;
  return defHttp.post({url: url, params});
}
